<?php
/**
 * Created by PhpStorm.
 * User: wufeng
 * Date: 2021/8/22
 * Time: 22:23
 */

namespace Wufeng\Healthkit;


use Wufeng\Healthkit\Auth\Authorize;

class AppClient
{
    private $config;
    private $token_expired_time;
    private $redirectUrl = "";

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * 获取授权链接
     * @return string
     * Time: 2021/8/23
     * Author: wufeng
     */
    public function authorizeCode(){
        $auth = new Authorize($this->config);
        $auth->setRedirectUrl($this->redirectUrl);
        return $auth->getCodeWebUrl();
    }

    /**
     * 获取accessToken
     * @param $code
     *
     * @return false|string
     * Time: 2021/8/23
     * Author: wufeng
     */
    public function getToken($code){
        $auth = new Authorize($this->config);
        $auth->setRedirectUrl($this->redirectUrl);
        return $auth->getTokenInfo($code);
    }

    private function isTokenExpired()
    {
        if (empty($this->accesstoken)) {
            return true;
        }
        if (time() > $this->token_expired_time) {
            return true;
        }
        return false;
    }
}
